import request from '../request'
import type { ApiResponse } from '../types'

export interface LoginParams {
  username: string
  password: string
}

export interface UserInfo {
  id: number
  username: string
  avatar: string
  roles: string[]
  discount: number
}

export interface CollectGoods {
  id: number
  createTime: string
  updateTime: string
  tenantId: number | null
  typeId: number
  title: string
  subTitle: string | null
  mainPic: string | null
  pics: string | null
  price: string
  sold: number
  content: string | null
  status: number
  sortNum: number
  goodsId: string
  entId: string
  goodsCode: string
  oracleGoodsName: string
  goodsSpec: string
  manufacturer: string
  unit: string
  approvalNo: string
  formula: string
  midpack: number
  qty: string
  planCate: string | null
  logogram: string
  wholLimtNum: number
  storNum: string
  isMidpackSaleDisabled: number
  gCategory: string
  qCategory: string
  oracleIsHidden: number
  isPres: number
  attribute: string
}

export interface CollectGoodsResponse {
  records: CollectGoods[]
  total: number
  size: number
  current: number
}

export const login = (data: LoginParams) => {
  return request.post<{ token: string }>('/app/user/login/password', data)
}

export const getUserInfo = () => {
  return request.get<ApiResponse<UserInfo>>('/app/user/info/person')
}

export const updatePasswordWithoutCode = (data: { oldPassword: string; newPassword: string }) => {
  return request.post<ApiResponse>('/app/user/info/updatePasswordWithoutCode', data)
}

export const getUserCollect = () => {
  return request.get<ApiResponse<CollectGoods[]>>('/app/collect/collect')
}

// 分页获取收藏商品列表
export const collectGoodsList = (params: { page: number; size: number }) => {
  return request.post<ApiResponse<CollectGoodsResponse>>('/app/user/collect/page', params)
}

// 获取单个商品的收藏状态
export const getGoodsCollect = (goodsId: number) => {
  return request.get<ApiResponse<{ id: number }>>(`/app/user/collect/detail/${goodsId}`)
}

// 收藏/取消收藏商品
export const toggleCollectGoods = (goodsId: number) => {
  return request.post<ApiResponse>('/app/user/collect/toggle', { goodsId })
}

// 记录足迹
export const recordFootprint = (goodsId: number) => {
  return request.post<ApiResponse>('/app/user/footprint/recordFootprint', { goodsId })
}

// 获取足迹列表
export const getFootprintList = (params: { page: number; size: number }) => {
  return request.post<any>('/app/user/footprint/page', {
    page: params.page,
    size: params.size,
    sort: 'desc',
    order: 'visitCount'
  })
}

// 删除足迹
export const deleteFootprint = (footprintId: number) => {
  return request.post<ApiResponse>('/app/user/footprint/delete', { footprintId })
}
